package lispinterpreter;

import java.util.LinkedList;

public class LispList {

    protected boolean isAtom;

    // Atributos
    public LinkedList<LispList> linkedList;

    public LispList() {
	linkedList = new LinkedList<LispList>();
	this.setAtom(false);
    }

    public void addElement(LispList l) {
	linkedList.add(l);
    }

    public LispList getElement(int index) {
	return linkedList.get(index);
    }

    public LispList getFirstElement() {
	return linkedList.getFirst();
    }

    public LispList getAndRemoveFirstElement() {
	return linkedList.removeFirst();
    }

    public int getSize() {
	return linkedList.size();
    }

    public boolean isAtom() {
	return isAtom;
    }

    /**
     * 
     * @pre LispList esta formado de atomos, sin operador.
     * @param tipo
     * @return
     */
    public boolean isParameter(int tipo) {
	boolean test = true;
	Object arreglo[] = linkedList.toArray();
	LispAtom lispObject;
	for (int i = 1; i < this.size(); i++) {
	    lispObject = (LispAtom) arreglo[i];
	    test = test && (tipo == lispObject.getType());
	}
	return test;
    }

    /**
     * Indica si la lista tiene lista anidadas. Es decir que todos los elementos
     * son átomso.
     * 
     * @returnadd
     */
    public boolean isSimple() {
	Object arreglo[] = linkedList.toArray();
	boolean test = true;
	LispList lispObject = new LispList();

	for (Object element : arreglo) {
	    lispObject = (LispList) element;
	    test = test && (lispObject.isAtom());
	}
	return test;
    }

    public void replaceElement(int index, LispList obj) {
	linkedList.set(index, obj);
    }

    public void setAtom(boolean isAtom) {
	this.isAtom = isAtom;
    }

    public int size() {
	return linkedList.size();
    }

    public void printLispList(LispList lista) {
	System.out.print("(");
	for (int i = 0; i < lista.size(); i++) {
	    LispList sublista = lista.getElement(i);
	    if (sublista.isAtom) {
		System.out.println();
	    }

	}
    }
}
